import MainController from "./MainController";
import Config from "./Config";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Ball extends cc.Component {

    isTouchGround: boolean = false;

    rigidBody: cc.RigidBody = null;
    main: MainController = null;

    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        this.rigidBody = this.getComponent(cc.RigidBody);
    }

    start() {

    }

    update(dt) {
        if (this.isTouchGround) {
            this.rigidBody.active = false;
            this.rigidBody.linearVelocity = cc.Vec2.ZERO;

            let pathPos: cc.Vec2[] = [];
            pathPos.push(this.node.position);
            pathPos.push(cc.v2(341, -481));
            pathPos.push(cc.v2(338, 610));
            pathPos.push(cc.v2(189, 559));

            this.node.runAction(cc.sequence(
                cc.cardinalSplineTo(2, pathPos, 1),
                cc.callFunc(() => {
                    this.rigidBody.active = true;
                    this.node.group = Config.groupBallInRecycle;
                    this.getComponent(cc.PhysicsCircleCollider).apply(); //代码里修改group，需要apply一下
                    this.main.recycleBall();
                })
            ));
            this.isTouchGround = false;
        }
    }


    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        if (otherCollider.node.name === 'ground') {
            this.isTouchGround = true;
        }
    }


}
